package com.kelvin.paopao.module.peisong.mypeisong.myorder;

import com.kelvin.paopao.api.RetrofitService;
import com.kelvin.paopao.api.responseBean.CommonListResponse;
import com.kelvin.paopao.api.responseBean.OrderModel;
import com.kelvin.paopao.module.base.IBasePresenter;
import com.kelvin.paopao.widgets.EmptyLayout;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by MCK on 2017/5/31.
 */

public class MyOrderPresenter implements IBasePresenter {
    private MyOrderFragment mView;

    public MyOrderPresenter(MyOrderFragment view) {
        this.mView = view;
    }

    @Override
    public void getData(final boolean isRefresh) {
        RetrofitService.getOrder().doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(@NonNull Disposable disposable) throws Exception {
                if (!isRefresh) {
                    mView.showLoading();
                }
            }
        }).subscribeOn(AndroidSchedulers.mainThread()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<CommonListResponse<OrderModel>>() {
                    @Override
                    public void accept(@NonNull CommonListResponse<OrderModel> orderModelCommonListResponse) throws Exception {
                        if (isRefresh) {
                            mView.finishRefresh();
                        } else {
                            mView.hideLoading();
                        }
                        if (orderModelCommonListResponse.getResult() != null
                                && orderModelCommonListResponse.getResult().size() != 0) {
                            mView.loadData(orderModelCommonListResponse.getResult());
                        } else {
                            mView.showNodata();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        mView.showNetError(new EmptyLayout.OnRetryListener() {
                            @Override
                            public void onRetry() {
                                getData(isRefresh);
                            }
                        });
                    }
                });
    }

    @Override
    public void getMoreData() {

    }
}
